/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nationsim.components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import nationsim.components.nation.Nation;
import nationsim.components.nation.NationStateDriver;
import nationsim.input.UserInput;
import nationsim.input.UserInputBarState;
import nationsim.input.UserInputReader;
import nationsim.simulation.NationSimulator;
import nationsim.ui.DisplayPanel;
import nationsim.ui.UIMenuBar;
import nationsim.ui.UIPanel;

/**
 * Class that handles are the interfacing between other classes.
 * @author CJ
 */
public class GameEngine implements ActionListener{

    protected Nation[] nationList;
    protected Nation playerNation;

    protected DisplayPanel dPanel;
    protected UIPanel uiPanel;
    protected UIMenuBar uiBar;

    protected UserInputBarState currentUserBarInput;

    public GameEngine(DisplayPanel dPanel, UIPanel uiPanel, UIMenuBar uiBar){
        this.dPanel = dPanel;
        this.uiPanel = uiPanel;
        this.uiBar = uiBar;
        this.currentUserBarInput = new UserInputBarState();
        initInterfacing();
        initNations();

        this.dPanel.refresh(nationList, playerNation);
    }

    protected void initNations(){
        this.playerNation = new Nation();
    }

    protected void initInterfacing(){
        this.uiPanel.addListener(this);
        this.uiBar.addListener(this);
    }

    public void actionPerformed(ActionEvent e){
        if(e.getActionCommand().equals("Submit"))
        {
            UserInput input = readUserNationState();
            if(UserInputReader.checkLegalTextValues(input)){
                NationStateDriver.setNationStateUserInput(this.playerNation, input);
                simulateTurn();
                updateUI();
                updateDisplay();
                System.out.println(this.playerNation.getState());
            }
        }else if(e.getSource() instanceof UIMenuBar){
            this.currentUserBarInput.update(e.getActionCommand());
        }
    }

    protected UserInput readUserNationState(){
        String[] input = this.uiPanel.getTextField();
        UserInput result = new UserInput();
        UserInputReader.readFromString(input, result);
        UserInputReader.readFromBar(this.currentUserBarInput, result);
        //Might want to parse the input into specific fields in the NationState
        
        return result;
    }

    protected void updateUI(){
        this.uiPanel.refresh(NationStateDriver.getNationState(this.playerNation));
        this.uiBar.refresh(this.currentUserBarInput);
    }

    protected void updateDisplay(){
        this.dPanel.refresh(this.nationList, this.playerNation);
    }

    protected void simulateTurn(){
        //Simulate Actions
        //NationSimulator.simluateAINations(this.nationList);

        //Simulate Results
        //for(Nation e: this.nationList)
         //   NationSimulator.simulateNationOutCome(e);
        NationSimulator.simulateNationOutCome(playerNation);
    }

}
